<template>
  <el-dialog
      :visible.sync="dialogVisible"
      title="关键字输入提示词条"
      width="30%"
      @close="handleClose"
  >
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="关键字">
        <el-input
            v-model="form.keyword"
            placeholder="请输入关键字"
            size="medium"
            @input="handleInput"
        />
      </el-form-item>
      <el-form-item label="经纬度">
        <el-input v-model="form.lat" size="medium"/>
        <el-input v-model="form.lng" size="medium"/>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="confirm">确定</el-button>
    </span>
  </el-dialog>
</template>

<script setup>
import {ref} from 'vue';
import {ElButton, ElDialog, ElForm, ElFormItem, ElInput} from 'element-plus';
import request from '@/utils/request';

const dialogVisible = ref(false);
const form = ref({
  keyword: '',
  lat: '',
  lng: ''
});

const handleClose = () => {
  form.value.keyword = '';
  form.value.lat = '';
  form.value.lng = '';
};

const handleInput = (value) => {
  if (value) {
    autocomplete(value);
  }
};

const autocomplete = (keyword) => {
  request({
    url: '/course/course/list',
    method: 'get',
    params: {keyword}
  }).then((response) => {
    console.log(response); // 处理响应数据
  }).catch((error) => {
    console.error(error);
  });
};

const confirm = () => {
  if (!form.value.keyword) {
    alert('请输入关键字');
    return;
  }
  localStorage.setItem('from', `${form.value.keyword}#${form.value.lng},${form.value.lat}`);
  history.back();
};
</script>

<style scoped>
#l-map {
  height: 800px;
  width: 100%;
}

#r-result {
  width: 100%;
  margin: 5px;
}
</style>